package com.sdy.supermarket.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.common.utils.MapUtil;
import com.sdy.common.utils.StringUtil;
import com.sdy.mvc.utils.JsonUtil;
import com.sdy.supermarket.biz.model.GsAppIpApply;
import com.sdy.supermarket.biz.service.GsAppIpApplyService;
import com.sdy.supermarket.biz.service.IpApplyService;
import com.sdy.supermarket.biz.vo.AppUpdateRequest;
import com.sdy.usercenter.api.UserQueryApi;
import com.sdy.usercenter.api.dto.UserDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class IpApplyServiceImpl implements IpApplyService {
    @Autowired
    private GsAppIpApplyService gsAppIpApplyService;
    @Autowired
    private UserQueryApi userQueryApi;
    
    @Override
    public IPage<GsAppIpApply> pageIpApply(Page<GsAppIpApply> page, Integer state, String applyNo, String resName) {
        IPage<GsAppIpApply> pageData = gsAppIpApplyService.page(new Page<>(page.getCurrent(), page.getSize()),
                new LambdaQueryWrapper<GsAppIpApply>()
                        .eq(state != null, GsAppIpApply::getState, state)
                        .eq(StringUtil.isNotBlank(applyNo), GsAppIpApply::getApplyNo, applyNo)
                        .like(StringUtil.isNotBlank(resName), GsAppIpApply::getContent, resName)
        );
        List<UserDTO> userDTOList = userQueryApi.listUser(pageData.getRecords()
                .stream()
                .map(GsAppIpApply::getUserId)
                .collect(Collectors.toList()));
        Map<Integer, UserDTO> userDTOMap = MapUtil.collectionToMap(userDTOList, UserDTO::getId);
        pageData.getRecords().forEach(item -> {
            UserDTO userDTO = userDTOMap.get(item.getUserId());
            if (userDTO != null) {
                item.setDeptName(userDTO.getOwnDeptName()).setUserName(userDTO.getName());
            }
            AppUpdateRequest req = JsonUtil.fromJson(item.getContent(), AppUpdateRequest.class);
            item.setIp(req.getIpUpdateSubRequestList()
                    .stream()
                    .map(AppUpdateRequest.IpUpdateSubRequest::getNewIp)
                    .collect(Collectors.joining(","))
            );
            item.setContent(null);
        });
        return pageData;
    }

    @Override
    public GsAppIpApply getIpApply(String applyNo) {
        GsAppIpApply apply = gsAppIpApplyService.getOne(new LambdaQueryWrapper<GsAppIpApply>().eq(GsAppIpApply::getApplyNo, applyNo));
        List<UserDTO> userDTOList = userQueryApi.listUser(Collections.singletonList(apply.getUserId()));
        UserDTO userDTO = userDTOList.get(0);
        if (userDTO != null) {
            apply.setDeptName(userDTO.getOwnDeptName()).setUserName(userDTO.getName());
        }
        AppUpdateRequest req = JsonUtil.fromJson(apply.getContent(), AppUpdateRequest.class);
        apply.setIp(req.getIpUpdateSubRequestList()
                .stream()
                .map(AppUpdateRequest.IpUpdateSubRequest::getNewIp)
                .collect(Collectors.joining(","))
        );
        apply.setContent(null);
        return apply;
    }
}
